package com.fit.pages;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.enums.AccountRole;
import com.fit.interfaces.CarI;
import com.fit.interfaces.User;
import com.fit.service.CarsI;
@SuppressWarnings("unused")
public class ViewCar {

	@SessionState
	private User user;
	
	private boolean userExists;

	public User getUser() {
		return user;
	}

	Object onActivate(){
		if(!userExists) return Login.class;
		return null;
	}
	
	
	@Inject
	@Property
	private CarsI cars;
	
	@Property
	private CarI chosenCar;
	
	@InjectPage
	private DisplayCar displayCarPage;
	
	@InjectPage
	private AddBorrow addBorrowPage;
	
	@InjectPage
	private EntryCarService entryCarServicePage;
	
	
	@OnEvent(component = "carDetailsPage")
	Object onActionLink(long id){
		CarI chosen = cars.retriveSingleCar(id);
		displayCarPage.setCar(chosen);
		return displayCarPage;
	}
	@OnEvent(component = "entryCarLink")
	Object onEntryLink(long id,String modelName,double price){
//		entryBorrow.setBorrowCar(modelName);
//		return entryBorrow;
		addBorrowPage.setBorrowCar_id(id);
		addBorrowPage.setBorrowcar_modelName(modelName);
		addBorrowPage.setPrice(price);
		return addBorrowPage;
	}
	@OnEvent(component = "entryCarService")
	Object onEntryServiceLink(long id,String modelname){
		entryCarServicePage.setServiceCar_id(id);
		entryCarServicePage.setServiceCar_modelName(modelname);
		return entryCarServicePage;
	}
	
	public boolean isUserAdmin(){
		if(user.getRole() == AccountRole.ADMINISTRATOR) return true;
		return false;
	}
	
	public boolean isUserAccount(){
		if(user.getRole() == AccountRole.USER) return true;
		return false;
	}
	
}
